Micron Document
`:top
In `F33f`_`[object-oriented programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object-oriented_programming]`_`f, an `!indexer`! allows instances of a particular class or struct to be indexed just like arrays.`:cite-ref-1[`F5bf`_`[1`#cite-note-1]`_`f] It is a form of `F33f`_`[operator overloading`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Operator_overloading]`_`f.

>>Contents

• `F0af`_`[Implementations`#implementations]`_`f
• `F0af`_`[C++`#c]`_`f
• `F0af`_`[C#`#c]`_`f
• `F0af`_`[PHP`#php]`_`f
• `F0af`_`[Python`#python]`_`f
• `F0af`_`[Rust`#rust]`_`f
• `F0af`_`[Smalltalk`#smalltalk]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[References`#references]`_`f

-─

>>Implementations

>>>C++

In C++ one can emulate indexing by overloading the `B100`F9d9[]`f`b operator. The expression `B100`F9d9a[b...]`f`b translates to a call to the user-defined function `B100`F9d9operator[]`f`b as `B100`F9d9(a).operator[](b...)`f`b.`:cite-ref-2[`F5bf`_`[2`#cite-note-2]`_`f] Here is an example,

`B100`F9d9struct vector {`f`b
`B100`F9d9 int size; double* data;`f`b
`B100`F9d9 vector(int n) { size = n; data = new double[n](); }`f`b
`B100`F9d9 ~vector(){ size = 0; delete[] data; }`f`b
`B100`F9d9 double& operator[](int i) { return data[i]; }`f`b
`B100`F9d9};`f`b
`B100`F9d9`f`b
`B100`F9d9#include <iostream>`f`b
`B100`F9d9`f`b
`B100`F9d9int main() {`f`b
`B100`F9d9 vector v(3);`f`b
`B100`F9d9 for (int i = 0; i < v.size; i++) v[i] = i + 1;`f`b
`B100`F9d9 for (int i = 0; i < v.size; i++) std::cout << v[i] << "\\n";`f`b
`B100`F9d9 return 0;`f`b
`B100`F9d9}`f`b

>>>C#

Indexers are implemented through the get and set `F33f`_`[accessors`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Mutator_method]`_`f for the `B100`F9d9operator[]`f`b. They are similar to `F33f`_`[properties`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Property_(programming)]`_`f, but differ by not being `F33f`_`[static`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Static_method]`_`f, and the fact that indexers' accessors take parameters. The get and set accessors are called as methods using the parameter list of the indexer declaration, but the set accessor still has the implicit `B100`F9d9value`f`b parameter.

>>>>Example 1

`B100`F9d9public class Vector`f`b
`B100`F9d9{`f`b
`B100`F9d9 private double[] _data;`f`b
`B100`F9d9`f`b
`B100`F9d9 public Vector(int n)`f`b
`B100`F9d9 {`f`b
`B100`F9d9 _data = new double[n];`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public int Size => _data.Length;`f`b
`B100`F9d9`f`b
`B100`F9d9 public double this[int i]`f`b
`B100`F9d9 {`f`b
`B100`F9d9 get => _data[i];`f`b
`B100`F9d9 set => _data[i] = value;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public static void Main()`f`b
`B100`F9d9 {`f`b
`B100`F9d9 var vector = new Vector(3);`f`b
`B100`F9d9 for (var i = 0; i < vector.Size; i++)`f`b
`B100`F9d9 vector[i] = i + 1;`f`b
`B100`F9d9 for (var i = 0; i < vector.Size; i++)`f`b
`B100`F9d9 System.Console.WriteLine(vector[i]);`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b

>>>>Example 2

Here is a C# example of the usage of an indexer in a class: `:cite-ref-3[`F5bf`_`[3`#cite-note-3]`_`f]

`B100`F9d9class Family`f`b
`B100`F9d9{`f`b
`B100`F9d9 private List<string> _familyMembers = new List<string>();`f`b
`B100`F9d9`f`b
`B100`F9d9 public Family(params string[] members)`f`b
`B100`F9d9 {`f`b
`B100`F9d9 _familyMembers.AddRange(members);`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public string this[int index]`f`b
`B100`F9d9 {`f`b
`B100`F9d9 // The get accessor`f`b
`B100`F9d9 get => _familyMembers[index];`f`b
`B100`F9d9`f`b
`B100`F9d9 // The set accessor with`f`b
`B100`F9d9 set => _familyMembers[index] = value;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public int this[string val]`f`b
`B100`F9d9 {`f`b
`B100`F9d9 // Getting index by value (first element found)`f`b
`B100`F9d9 get => _familyMembers.FindIndex(m => m == val);`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public int Length => _familyMembers.Count;`f`b
`B100`F9d9}`f`b

Usage example:

`B100`F9d9void Main()`f`b
`B100`F9d9{`f`b
`B100`F9d9 var doeFamily = new Family("John", "Jane");`f`b
`B100`F9d9 for (int i = 0; i < doeFamily.Length; i++)`f`b
`B100`F9d9 {`f`b
`B100`F9d9 var member = doeFamily[i];`f`b
`B100`F9d9 var index = doeFamily[member]; // same as i in this case, but it demonstrates indexer overloading allowing to search doeFamily by value.`f`b
`B100`F9d9 Console.WriteLine($"{member} is the member number {index} of the {nameof(doeFamily)}");`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b

In this example, the indexer is used to get the value at the nth position, and then to get the position in the list referenced by its value. The output of the code is:

`B100`F9d9John is the member number 0 of the doeFamily`f`b
`B100`F9d9Jane is the member number 1 of the doeFamily`f`b

>>>PHP

In `F33f`_`[PHP`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=PHP]`_`f indexing can be implemented via the predefined `B100`F9d9ArrayAccess`f`b interface,`:cite-ref-4[`F5bf`_`[4`#cite-note-4]`_`f]

`B100`F9d9class Vector implements ArrayAccess`f`b
`B100`F9d9{`f`b
`B100`F9d9 function __construct(int $n) {`f`b
`B100`F9d9 $this->size = $n;`f`b
`B100`F9d9 $this->data = array_fill(0, $n, 0);`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public function offsetGet($offset): mixed {`f`b
`B100`F9d9 return $this->data[$offset];`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public function offsetSet($offset, $value): void {`f`b
`B100`F9d9 $this->data[$offset] = $value;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public function offsetExists($offset): bool {}`f`b
`B100`F9d9`f`b
`B100`F9d9 public function offsetUnset($offset): void {}`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9$vector = new Vector(3);`f`b
`B100`F9d9`f`b
`B100`F9d9for ($i = 0; $i < $vector->size; $i++) $vector[$i] = $i + 1;`f`b
`B100`F9d9for ($i = 0; $i < $vector->size; $i++) print "{$vector[$i]}\\n";`f`b

>>>Python

In `F33f`_`[Python`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Python_(programming_language)]`_`f one implements indexing by overloading the `B100`F9d9__getitem__`f`b and `B100`F9d9__setitem__`f`b methods,

`B100`F9d9import array`f`b
`B100`F9d9`f`b
`B100`F9d9class Vector(object):`f`b
`B100`F9d9 def __init__(self, n: int):`f`b
`B100`F9d9 self.size = n`f`b
`B100`F9d9 self.data = array.array("d", [0.0] * n)`f`b
`B100`F9d9`f`b
`B100`F9d9 def __getitem__(self, i: int):`f`b
`B100`F9d9 return self.data[i]`f`b
`B100`F9d9`f`b
`B100`F9d9 def __setitem__(self, i: int, value):`f`b
`B100`F9d9 self.data[i] = value`f`b
`B100`F9d9`f`b
`B100`F9d9vector = Vector(3)`f`b
`B100`F9d9for i in range(vector.size):`f`b
`B100`F9d9 vector[i] = i + 1`f`b
`B100`F9d9for i in range(vector.size):`f`b
`B100`F9d9 print(vector[i])`f`b

>>>Rust

`F33f`_`[Rust`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Rust_(programming_language)]`_`f provides the std::ops::Index trait.`:cite-ref-5[`F5bf`_`[5`#cite-note-5]`_`f]

`B100`F9d9use std::ops::Index;`f`b
`B100`F9d9`f`b
`B100`F9d9enum Nucleotide {`f`b
`B100`F9d9 A,`f`b
`B100`F9d9 C,`f`b
`B100`F9d9 G,`f`b
`B100`F9d9 T,`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9struct NucleotideCount {`f`b
`B100`F9d9 a: usize,`f`b
`B100`F9d9 c: usize,`f`b
`B100`F9d9 g: usize,`f`b
`B100`F9d9 t: usize,`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9impl Index<Nucleotide> for NucleotideCount {`f`b
`B100`F9d9 type Output = usize;`f`b
`B100`F9d9`f`b
`B100`F9d9 fn index(&self, nucleotide: Nucleotide) -> &Self::Output {`f`b
`B100`F9d9 match nucleotide {`f`b
`B100`F9d9 Nucleotide::A => &self.a,`f`b
`B100`F9d9 Nucleotide::C => &self.c,`f`b
`B100`F9d9 Nucleotide::G => &self.g,`f`b
`B100`F9d9 Nucleotide::T => &self.t,`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12};`f`b
`B100`F9d9assert_eq!(nucleotide_count[Nucleotide::A], 14);`f`b
`B100`F9d9assert_eq!(nucleotide_count[Nucleotide::C], 9);`f`b
`B100`F9d9assert_eq!(nucleotide_count[Nucleotide::G], 10);`f`b
`B100`F9d9assert_eq!(nucleotide_count[Nucleotide::T], 12);`f`b

>>>Smalltalk

In `F33f`_`[Smalltalk`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Smalltalk]`_`f one can emulate indexing by (e.g.) defining the `B100`F9d9get:`f`b and `B100`F9d9set:value:`f`b instance methods. For example, in `F33f`_`[GNU Smalltalk`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=GNU_Smalltalk]`_`f,

`B100`F9d9Object subclass: vector [ |data| ]`f`b
`B100`F9d9vector class extend [ new: n [ |v| v:=super new. v init: n. ^v] ]`f`b
`B100`F9d9vector extend [ init: n [ data:= Array new: n ] ]`f`b
`B100`F9d9vector extend [ size [ ^(data size) ] ]`f`b
`B100`F9d9vector extend [ get: i [ ^(data at: i) ] ]`f`b
`B100`F9d9vector extend [ set: i value: x [ data at: i put: x ] ]`f`b
`B100`F9d9v:=vector new: 3`f`b
`B100`F9d91 to: (v size) do: [:i| v set: i value: (i+1) ]`f`b
`B100`F9d91 to: (v size) do: [:i| (v get: i) printNl ]`f`b

>>See also

• `F33f`_`[Mutator method`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Mutator_method]`_`f

>>References

`:cite-note-1`!1.`! `F0af`_`[↑`#cite-ref-1]`_`f `:citerefjagadish9802008`ajagadish980 (2008-01-29). "C# - What is an indexer in C#". `*SURESHKUMAR.NET FORUMS`*. Archived from the original on September 22, 2009. Retrieved 2011-08-01.`B100`F9d9{{cite web}}`f`b: CS1 maint: numeric names: authors list (link)
`:cite-note-2`!2.`! `F0af`_`[↑`#cite-ref-2]`_`f "C++ operator overloading".
`:cite-note-3`!3.`! `F0af`_`[↑`#cite-ref-3]`_`f "C# Interview Questions". `*.net Funda`*. Retrieved 2011-08-01.
`:cite-note-4`!4.`! `F0af`_`[↑`#cite-ref-4]`_`f "PHP ArrayAccess interface".
`:cite-note-5`!5.`! `F0af`_`[↑`#cite-ref-5]`_`f "Index in std::ops - Rust". `*doc.rust-lang.org`*. Retrieved 11 January 2025.

`c`F0af`_`[↑ Back to top`#top]`_`f`a